<?php
class Apanel extends CI_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->load->library('session');
		$this->load->library('form_validation');
		$this->load->library('cismarty');
		$this->load->model("apanel/template");
		$this->load->model("apanel/db_log");
		$this->load->model("apanel/db_member");
		//$this->load->library('pagination');
	}
	
	public function index(){
		$fail	= "";
		if($this->input->post('btnlogin') != null){
			$user 	= $this->input->post('username');
			$pass	= $this->input->post("password");
			if($user != "" && $pass != "" ){
				$pass = md5($pass);
				$sql = "select * from mobile_user where username=? and password=?";
				$res = $this->db->query($sql,array($user,$pass));
				$num = $res->num_rows();
				if($num > 0){
					// create session
					$mobile = $res->row();
					// user access
					$this->db->select('t3.name as partner_name,t2.pkid,t2.name as c_name');
					$this->db->from("mobile_user t1");
					$this->db->join("mobile_customer t2","t1.customer_pkid=t2.pkid");
					$this->db->join('mobile_user_access t3','t3.pkid = t1.role_pkid');
					$this->db->where('t1.username',$mobile->username);
					$arr_access  = $this->db->get()->row();
					//print_r($arr_access);
					$ss = array("sess_login"=>'y',
							"sess_username"=>$mobile->username,
							"sess_customer_id"=>$arr_access->pkid,
							"sess_customer_name"=>$arr_access->c_name,
							"sess_userid"=>$mobile->pkid,
							"sess_hname"=>$mobile->name,
							"sess_access"=>$arr_access->partner_name
					);
					$this->session->set_userdata($ss);
					$this->db_log->logs();
					redirect('apanel/dashboard');
				}		
			}else {
				$fail  = "Please insert Username,Password";
				$this->cismarty->assign("errors",$fail);
			}
		} 
		//== template
		$this->cismarty->assign("form_open",form_open('apanel/index','class="mws-form"'));
		$this->cismarty->assign("form_close",form_close());
		$this->cismarty->assign("url",base_url());
		$this->cismarty->display('apanel/login.html');
	}
	
	public function logout(){
		$this->session->unset_userdata("sess_login");
		redirect("apanel","refresh");
		exit();
	}
	
	public function dashboard(){
		if($this->session->userdata('sess_login') == null) 
				redirect('apanel');
		$scripts = '
		<script type="text/javascript" charset="utf-8">
		$(document).ready(function() {
		$(\'#table1\').dataTable( {
		"bJQueryUI": true,
		"bProcessing": true,
		"bServerSide": true,
		"sAjaxSource": "'.base_url().'manage_member/datalist",
		"iDisplayStart"  : 0,
		"sPaginationType": "full_numbers",
		"aaSorting": [[ 0, "desc" ]],
		"aoColumnDefs": [
		{ "bSortable": false, "aTargets": [ 0 ] },
		],
		"fnServerData": function(sSource, aoData, fnCallback)
		{
		$.ajax(
		{
		\'dataType\': \'json\',
		\'type\'  : \'POST\',
		\'url\'    : sSource,
		\'data\'  : aoData,
		\'success\' : fnCallback
	}
	);
	}
	} );
	} );
	</script>';
		$this->cismarty->assign("scripts",$scripts);
		//== template
		$this->cismarty->assign("log",$this->report_logs());
		$this->cismarty->assign("header",$this->template->header());
		$this->cismarty->assign("menu",$this->template->menu(1));
		$this->cismarty->assign("footer",$this->template->footer());
		$this->cismarty->assign("url",base_url());
		$this->cismarty->assign("form_open",form_open('manage_member/member_delAll'));
		$this->cismarty->assign("form_close",form_close());
		$this->cismarty->display('apanel/dashboard.html');
	}
	
	private function report_logs(){
		if($this->session->userdata('sess_login') == null)
			redirect('apanel');
		$this->db->limit("10");
		$this->db->order_by("t1.pkid",'desc');
		$this->db->from("mobile_user_log t1");
		$this->db->join("mobile_user t2","t1.username=t2.username","left" );
		if($this->session->userdata("sess_customer_name") != "eshine"){
			$this->db->where("t2.customer_pkid",$this->session->userdata("sess_customer_id"));
		}
		$res = $this->db->get();
		$arrlog = $res->result_array() ;
// 		foreach ($res->result_array() as $log){
// 			$tmp['admin']		=$log['username'];
// 			$tmp['ip']				=$log['ipaddress'];
// 			$tmp['date']			=$log['login_date'];
// 			$arrlog[] = $tmp;
// 		}
		return $arrlog;
	}
	
	/*private function last_bill(){
		$num = $this->db->count_all("hotel_customer_billing");
		$this->db->select('t1.name,t2.pkid,t2.total_bill,t2.total_paid,t2.payment_method,t2.created_date');
		$this->db->from("hotel_customer t1");
		$this->db->join('hotel_customer_billing t2','t1.pkid = t2.customer_pkid');
		$this->db->limit("10");
		$this->db->order_by("t2.pkid", "desc");
		$res = $this->db->get();
		$arrb = $res->result_array();
		return array($arrb,$num);
	}*/
	
	public function order_all(){
		$num = $this->db->count_all("hotel_order");
		$this->db->select('t2.name as cname,t1.pkid,t1.price,t1.qty,t1.total_amount,t3.name_en as pname,t1.updated_by,t1.updated_date,
									t4.room_no,t1.type,t1.event_status,t1.created_date');
		$this->db->from("hotel_order t1");
		$this->db->join('hotel_customer t2','t1.customer_pkid = t2.pkid','left');
		$this->db->join('hotel_product_item t3','t3.pkid = t1.txn_pkid','left');
		$this->db->join('hotel_room t4','t4.pkid = t1.room_pkid','left');
		$this->db->limit(10);
		$this->db->order_by("t1.pkid", "desc");
		$res = $this->db->get();
		//==
		$arr = $res->result_array();
		$cat = array();
		foreach ($arr as $P){
			$tmp['pkid']						=$P['pkid'];
			$tmp['cname']					=$P['cname'];
			$tmp['pname']					=$P['pname'];
			$tmp['type']						=$P['type'];
			$tmp['qty']							=$P['qty'];
			$tmp['price']						=$P['price'];
			$tmp['room']						=$P['room_no'];
			$tmp['total_amount']			=$P['total_amount'];
			$tmp['date']						=$P['created_date'];
			$tmp['update']					=$P['updated_date'];
			$str_status = "";
			if($P['event_status'] == 'wait'){
				$str_status .='<option value="wait-'.$P['pkid'].'" id="wait" selected >Pending</option>';
				$str_status .='<option value="inprogres-'.$P['pkid'].'" id="inprogres"  >In Progres</option>';
				$str_status .='<option value="success-'.$P['pkid'].'" id="success" >Successfull</option>';
			}else if($P['event_status'] == 'inprogres'){
				$str_status .='<option value="wait-'.$P['pkid'].'" id="wait"  >Pending</option>';
				$str_status .='<option value="inprogres-'.$P['pkid'].'" id="inprogres"  selected>In Progres</option>';
				$str_status .='<option value="success-'.$P['pkid'].'" id="success" >Successfull</option>';
			}else if($P['event_status'] == 'success'){
				$str_status .='<option value="wait-'.$P['pkid'].'"  id="wait" >Pending</option>';
				$str_status .='<option value="inprogres-'.$P['pkid'].'" id="inprogres"  >In Progres</option>';
				$str_status .='<option value="success-'.$P['pkid'].'" id="success"  selected>Successfull</option>';
			}else {
				$str_status .='<option value="wait-'.$P['pkid'].'"  id="wait" >Pending</option>';
				$str_status .='<option value="inprogres-'.$P['pkid'].'" id="inprogres"  >In Progres</option>';
				$str_status .='<option value="success-'.$P['pkid'].'" id="success" >Successfull</option>';
			}
			if($P['event_status'] == "wait"){
				$bgcolor='bgcolor="#66CCCC"';
			}else {
				$bgcolor = "";
			}
			$tmp['bg']							=$bgcolor;
			$tmp['status']						=$str_status;
			$tmp['process_by']				=$this->gen_user($P['updated_by']);
			$cat[] = $tmp;
		}
		return array($cat,$num);
	}
	
	/*private function last_shoping_bill(){
		$num = $this->db->count_all("hotel_shopping_billing");
		$this->db->select('t2.name as cname,t1.pkid,t1.total_bill,t1.total_paid,t1.payment_method,t1.created_date');
		$this->db->from("hotel_shopping_billing t1");
		$this->db->join('hotel_customer t2','t1.customer_pkid = t2.pkid','left');
		$this->db->limit("10");
		$this->db->order_by("t1.pkid", "desc");
		$res = $this->db->get();
		$arrorder = $res->result_array();
		return array($arrorder,$num);
	}*/
	
	private function shoping_all(){
			$num = $this->db->count_all("hotel_shopping_cart");
			$this->db->select('t2.name as cname,t1.pkid,t1.price,t1.qty,t1.total_amount,t3.name_en as pname,t1.updated_by,t1.updated_date,
										t4.room_no,t1.event_status,t1.created_date');
			$this->db->from("hotel_shopping_cart t1");
			$this->db->join('hotel_customer t2','t1.customer_pkid = t2.pkid','left');
			$this->db->join('hotel_product_item t3','t3.pkid = t1.txn_pkid','left');
			$this->db->join('hotel_room t4','t4.pkid = t1.room_pkid','left');
			$this->db->limit(10);
			$this->db->order_by("t1.pkid", "desc");
			$res = $this->db->get();
		//	print_r($res);
			//==
			$arr = $res->result_array();
			$cat = array();
			foreach ($arr as $P){
				$tmp['pkid']						=$P['pkid'];
				$tmp['cname']					=$P['cname'];
				$tmp['pname']					=$P['pname'];
				$tmp['qty']							=$P['qty'];
				$tmp['price']						=$P['price'];
				$tmp['room']						=$P['room_no'];
				$tmp['total_amount']			=$P['total_amount'];
				$tmp['date']						=$P['created_date'];
				$tmp['update']					=$P['updated_date'];
				$str_status = "";
				if($P['event_status'] == 'wait'){
					$str_status .='<option value="wait-'.$P['pkid'].'" id="wait" selected >Pending</option>';
					$str_status .='<option value="inprogres-'.$P['pkid'].'" id="inprogres"  >In Progres</option>';
					$str_status .='<option value="success-'.$P['pkid'].'" id="success" >Successfull</option>';
				}else if($P['event_status'] == 'inprogres'){
					$str_status .='<option value="wait-'.$P['pkid'].'" id="wait"  >Pending</option>';
					$str_status .='<option value="inprogres-'.$P['pkid'].'" id="inprogres"  selected>In Progres</option>';
					$str_status .='<option value="success-'.$P['pkid'].'" id="success" >Successfull</option>';
				}else if($P['event_status'] == 'success'){
					$str_status .='<option value="wait-'.$P['pkid'].'"  id="wait" >Pending</option>';
					$str_status .='<option value="inprogres-'.$P['pkid'].'" id="inprogres"  >In Progres</option>';
					$str_status .='<option value="success-'.$P['pkid'].'" id="success"  selected>Successfull</option>';
				}else {
					$str_status .='<option value="wait-'.$P['pkid'].'"  id="wait" >Pending</option>';
					$str_status .='<option value="inprogres-'.$P['pkid'].'" id="inprogres"  >In Progres</option>';
					$str_status .='<option value="success-'.$P['pkid'].'" id="success" >Successfull</option>';
				}
				if($P['event_status'] == "wait"){
					$bgcolor='bgcolor="#66CCCC"';
				}else {
					$bgcolor = "";
				}
				$tmp['bg']							=$bgcolor;
				$tmp['status']						=$str_status;
				$tmp['process_by']				=$this->gen_user($P['updated_by']);
				$cat[] = $tmp;
			}
			//print_r($cat);
			return array($cat,$num);
		}
		
		private function gen_user($pkid=""){
			if($pkid != ""){
			$this->db->where("pkid",$pkid);
			$row = $this->db->get("hotel_user")->row();
			return $row->name;
			}else return false;
		}
		
	public function changes_cart($id){
		list($status,$pkid) = explode("-", $id);
		$this->db->where("pkid",$pkid);
		$this->db->update("hotel_shopping_cart",array("event_status"=>$status,
									'updated_date'=>date('Y-m-d H:i:s'),
									'updated_by'=>$this->session->userdata("sess_userid")
								));
		echo "alert('update $status');";
		echo "window.location='dashboard';";
	}
	
	public function changes_order($id){
		list($status,$pkid) = explode("-", $id);
		$this->db->where("pkid",$pkid);
		$this->db->update("hotel_order",array("event_status"=>$status,
																	'updated_date'=>date('Y-m-d H:i:s'),
																	'updated_by'=>$this->session->userdata("sess_userid")
														));
		echo "alert('update $status');";
		echo "window.location='dashboard';";
	}
	
	public function member_list($st=""){
		if($this->session->userdata('sess_login') == null)
			redirect('apanel');
		//list($item,$num,$page,$total) = $this->listall();
		//$this->cismarty->assign("total",$total);
		if($st == "success"){
			$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
		}
		$scripts = '
		<script type="text/javascript" charset="utf-8">
		$(document).ready(function() {
		$(\'#table1\').dataTable( {
		"bJQueryUI": true,
		"bProcessing": true,
		"bServerSide": true,
		"sAjaxSource": "'.base_url().'manage_member/datalist",
		"iDisplayStart"  : 0,
		"sPaginationType": "full_numbers",
		"aaSorting": [[ 1, "desc" ]],
		"aoColumnDefs": [
		{ "bSortable": false, "aTargets": [ 0 ] },
		],
		"fnServerData": function(sSource, aoData, fnCallback)
		{
		$.ajax(
		{
		\'dataType\': \'json\',
		\'type\'  : \'POST\',
		\'url\'    : sSource,
		\'data\'  : aoData,
		\'success\' : fnCallback
	}
	);
	}
	} );
	} );
	</script>';
		$this->cismarty->assign("scripts",$scripts);
		//== template
		$this->cismarty->assign("header",$this->template->header());
		$this->cismarty->assign("menu",$this->template->menu(1,2));
		$this->cismarty->assign("footer",$this->template->footer());
		$this->cismarty->assign("url",base_url());
		$this->cismarty->assign("form_open",form_open('manage_member/member_delAll'));
		$this->cismarty->assign("form_close",form_close());
		$this->cismarty->display('apanel/member/member_list.html');
	}
	
	public function datalist(){
		$output = $this->db_member->datalist();
		echo json_encode($output);
	}
	
	public function member_detail($id){
			
			$this->db->select("t1.pkid,t1.name,t1.email,t1.mobile,t1.imei,t2.card_no");
			$this->db->from("mobile_member t1");
			$this->db->join("mobile_card t2","t1.card_pkid = t2.pkid");
			$this->db->where("t1.pkid",$id);
			$res =$this->db->get()->row();
			/*foreach ($res as $P){
				$tmp['pkid']		=$P['pkid'];
				$tmp['name']		=$P['name'];
				$tmp['email']		=$P['email'];
				$tmp['mobile']		=$P['mobile'];
				$tmp['imei']		=$P['imei'];
				$tmp['card_no']		=$P['card_no'];
				$arr[] =$tmp;
			}*/
		
			$this->cismarty->assign("name",$res->name);
			$this->cismarty->assign("email",$res->email);
			$this->cismarty->assign("mobile",$res->mobile);
			$this->cismarty->assign("imei",$res->imei);
			$this->cismarty->assign("card_no",$res->card_no);
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(1,2));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("id",$id);
			$this->cismarty->assign("form_open",form_open_multipart("apanel/member_detail/$id"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/member/member_view.html');
		}
}
?>
